package cn.pengyinrui.department.system.emp.controller;

import cn.pengyinrui.department.system.dept.domain.Department;
import cn.pengyinrui.department.system.emp.service.IEmployeeService;
import cn.pengyinrui.department.system.emp.domain.Employee;
import cn.pengyinrui.department.system.emp.query.EmployeeQuery;
import cn.pengyinrui.department.utils.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.data.redis.core.RedisTemplate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
// 手动修改路径
import cn.pengyinrui.department.utils.AjaxResult;
import cn.pengyinrui.department.utils.BusinessException;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/emp")
@Api(value = "XXX的API",description = "XXX相关的CRUD功能")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
* 查看所有的员工信息
* @return
*/
    @GetMapping()
    @ApiOperation(value = "查询全部的信息")
    public AjaxResult list(){
        try {
            return AjaxResult.success()
                    .setData(employeeService.findAll())
                    .setTotal(employeeService.findAllByQueryCount(new EmployeeQuery()));
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.error().setTip(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setTip("网络异常，请稍后再试");
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/query")
    @ApiOperation(value = "高级查询，分页")
    public AjaxResult query(@RequestBody EmployeeQuery query){
        try {
            return AjaxResult.success()
                    .setData(employeeService.findAllByQuery(query))
                    .setTotal(employeeService.findAllByQueryCount(query));
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.error().setTip(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setTip("网络异常，请稍后再试");
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public Employee get(@PathVariable("id")Long id){
        return employeeService.findOne(id);
    }

    /**
     * 保存和修改公用的
     * @param employee  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @ApiOperation(value = "保存，传入id修改，不传入id新增")
    public AjaxResult save(@RequestBody Employee employee){
        try {
            if (employee.getId() != null) {
                employeeService.update(employee);
            } else {
                employeeService.add(employee);
            }
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.error().setTip(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setTip("网络异常，请稍后再试");
        }
        return AjaxResult.success();
    }

    /**
     * 删除对象信息
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过id删除")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            employeeService.delete(id);
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.error().setTip(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setTip("网络异常，请稍后再试");
        }
        return AjaxResult.success();
    }

    @GetMapping("/export")
    @ApiOperation(value = "员工信息导出")
    public void export(HttpServletResponse response){
        List<Employee> all = employeeService.findAll();
        ExcelUtil.exportExcel(all,"员工信息","员工信息",Employee.class,"员工信息统计.xls",response);
    }

    @PostMapping("/importExcel")
    @ApiOperation(value = "通过xls表格导入员工信息")
    public void importExcel(@RequestPart("file") MultipartFile file){
        List<Employee> departments = ExcelUtil.importExcel(file, 1, 1, Employee.class);
        // 遍历并添加到数据库
        departments.forEach(System.out::println);
    }

}
